<template>
    <el-dialog :title="titleMap[mode]" v-model="visible" :width="1000" destroy-on-close @closed="$emit('closed')" align-center :close-on-click-modal="false" :close-on-press-escape="mode != 'show' ? false : true">
        <el-form :model="form" :rules="rules" :disabled="mode=='show'" label-width="130" ref="dialogForm">
            <el-col :xs="24" :sm="12">
                <el-form-item label="检验单号" prop="test_code">
                    <el-input v-model="purchase_test_code" readonly></el-input>
                </el-form-item>
            </el-col>
            <el-row :gutter="1">
                <el-col :xs="24" :span="12">
                    <el-form-item label="操作" prop="action_type">
                        <el-select v-model="form.action_type" placeholder="请输入" filterable clearable>
                            <el-option label="最终检验" :value="1"></el-option>
                            <el-option label="跑米" :value="2"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :span="12">
                    <el-form-item label="日期" prop="action_date">
                        <el-date-picker v-model="form.action_date" type="date" placeholder="请选择日期" :value-format="'YYYY-MM-DD'" style="width:100%" disabled />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :xs="24" :span="12">
                    <el-form-item label="批号" prop="batch_num">
                        <el-input v-model="batch_num" readonly></el-input>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :span="12">
                    <el-form-item label="计重" prop="weight">
                        <el-input v-model="form.weight" clearable placeholder="" @input="weightInput" @focus="common.inputFocus($event)"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :xs="24" :span="12">
                    <el-form-item label="品名" prop="goods_name">
                        <el-input v-model="form.goods_name" readonly></el-input>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :span="12">
                    <el-form-item label="计数" prop="number">
                        <el-input v-model="form.number" clearable @input="numberInput" @focus="common.inputFocus($event)"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :xs="24" :span="12">
                    <el-form-item label="入库单号" prop="purchase_ware_id">
                        <el-select v-model="form.purchase_ware_id" placeholder="请选择" clearable @change="testChange">
                            <el-option v-for="item in houseList" :key="item.id" :label="item.purchase_ware_code" :value="item.id"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :span="12">
                    <el-form-item label="检验数量" prop="check_num">
                        <el-input v-model="form.check_num" clearable readonly></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :xs="24" :span="12">
                    <el-form-item label="折码" prop="folding_id">
                        <el-select v-model="form.folding_id" placeholder="请选择" filterable clearable @change="foldChange">
                            <el-option v-for="item in foldingList" :key="item.id" :label="item.name" :value="item.id"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :span="12">
                    <el-form-item label="" prop="type" label-width="70">
                        <el-radio-group v-model="form.type" @change="typeChange">
                            <el-radio-button  :label="1" size="large">正常</el-radio-button>
                            <el-radio-button  :label="2" size="large">合成</el-radio-button>
                            <el-radio-button  :label="3" size="large">1分2</el-radio-button>
                            <el-radio-button  :label="4" size="large">1分3</el-radio-button>
                        </el-radio-group>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :xs="24" :span="12">
                    <el-form-item label="Color" prop="color_id">
                        <el-select v-model="form.color_id" placeholder="请选择" filterable clearable>
                            <el-option v-for="item in colorList" :key="item.id" :label="item.name" :value="item.id"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :span="12">
                    <el-form-item label="消耗数量" prop="use_num">
                        <el-input v-model="form.use_num" clearable :readonly="form.type != 2"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :xs="24" :span="12">
                    <el-form-item label="颜色" prop="color">
                        <el-select v-model="form.color" placeholder="请选择" filterable clearable @change="colorChange">
                            <el-option v-for="(item,index) in samplecolorList" :key="index" :label="item.name" :value="item.name"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :span="12">
                    <el-form-item label="净重" prop="net_weight">
                        <el-input v-model="form.net_weight" readonly></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :xs="24" :span="12">
                    <el-form-item label="花型" prop="flower">
                        <el-select v-model="form.flower" placeholder="请选择" filterable clearable @change="flowerChange">
                            <el-option v-for="(item,index) in flowerList" :key="index" :label="item.flower" :value="item.flower"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :span="12">
                    <el-form-item label="米数" prop="meter">
                        <el-input v-model="form.meter" readonly></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :xs="24" :span="12">
                    <el-form-item label="来数" prop="come">
                        <el-input v-model="form.come" clearable ref="comeInput" type="number" @mousewheel.prevent @keyup.enter="comeEnter" @focus="common.inputFocus($event)"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :span="12">
                    <el-form-item label="码数" prop="code_number">
                        <el-input v-model="form.code_number" clearable @focus="common.inputFocus($event)"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :xs="24" :span="12">
                    <el-form-item label="剪数" prop="cut">
                        <el-input v-model="form.cut" clearable ref="cutInput" type="number" @mousewheel.prevent @input="cutInputChange" @keyup.enter="is_dialog" @focus="common.inputFocus($event)"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :span="12">
                    <el-form-item label="克重" prop="gram_weight">
                        <el-input v-model="form.gram_weight" clearable @focus="common.inputFocus($event)"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :xs="24" :span="12">
                    <el-form-item label="次品种类" prop="reject_cate_id">
                        <el-select v-model="form.reject_cate_id" placeholder="请选择" filterable clearable>
                            <el-option v-for="item in rejectList" :key="item.id" :label="item.name" :value="item.id"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <template #footer>
            <el-button @click="visible=false">取 消</el-button>
            <el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit">保 存</el-button>
        </template>
    </el-dialog>
    <el-dialog :title="dialogTitle" v-model="dialog.dialog" align-center :width="500">
        <template #footer>
			<el-button @click="dialog.dialog = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
    </el-dialog>
</template>

<script>
export default {
    emits: ['success', 'closed'],
    data() {
        return {
            mode: "add",
            titleMap: {
                add: '新增检验',
                edit: '编辑检验',
                show: '查看检验',
            },
            visible: false,
            isSaveing: false,
            dialog: {
                house: false,
                dialog: false,
            },
            form: {
                id: "",
                purchase_test_id: "",
                action_date: this.moment().format('YYYY-MM-DD'),
                purchase_ware_id: "",
                ware_detail_id: "",
                goods_code: "",
                goods_name: "",
                goods_id: "",
                folding_id: "",
                color_id: "",
                color: "",
                flower: "",
                flower_image: "",
                code_number: "",
                gram_weight: "",
                warehouseids: "",
                check_num: "",
                type: 1,
                net_weight: "",
                meter: "",
                use_num: 1,
                come: 0,
                cut: 0,
                weight: "",
                number: "",
                remark: "",
                action_type: 1,
                work_uids: "",
                machine_id: "",
                reject_cate_id: "",
                reject_num: 2,
                reject_type: 1,
            },
            rules: {
                plan_name: [
                    {required: true, message: '请输入采购计划名称'}
                ],
            },
            foldingList: [],
            colorList: [],
            rejectList: [],
            samplecolorList: [],
            flowerList: [],
            up_limit: "",
            down_limit: "",
            purchase_test_code: "",
            houseList: [],
            dialogTitle: "",
            batch_num: "",
            read_number: "",
        }
    },
    mounted() {
        this.getFolding()
        this.getColor()
        this.getReject()
    },
    methods: {
        //显示
        open(mode = 'add') {
            this.mode = mode;
            this.visible = true;
            return this
        },
        //表单注入数据
        async setData(data) {
            this.form.id = data.id
            var res = await this.$API.business.inspect.execute.detail.get({id: data.id});
            this.form.purchase_test_id = res.data.purchase_test_id
            this.purchase_test_code = res.data.test?.test_code
            this.form.action_date = res.data.action_date
            this.form.purchase_ware_id = Number(res.data.purchase_ware_id)
            this.form.ware_detail_id = res.data.ware_detail_id
            this.form.goods_code = res.data.goods_code
            this.form.goods_name = res.data.goods_name
            this.form.goods_id = res.data.goods_id
            this.form.folding_id = res.data.folding_id
            this.batch_num = res.data.batch_num
            this.form.color_id = res.data.color_id
            this.form.color = res.data.color
            this.form.flower = res.data.flower
            this.form.flower_image = res.data.flower_image
            this.form.code_number = res.data.code_number
            this.form.gram_weight = res.data.gram_weight
            this.form.warehouseids = res.data.warehouseids
            this.form.check_num = res.data.check_num
            this.form.type = res.data.type
            this.form.net_weight = res.data.net_weight
            this.form.meter = res.data.meter
            this.form.use_num = res.data.use_num
            this.form.come = res.data.come
            this.form.cut = res.data.cut
            this.form.weight = res.data.weight
            this.form.number = res.data.number
            this.read_number = res.data.number
            this.form.remark = res.data.remark
            this.form.action_type = res.data.action_type
            this.form.work_uids = res.data.work_uids.split(',')
            this.form.machine_id = res.data.machine_id
            this.form.reject_cate_id = res.data.reject_cate_id
            this.form.reject_num = res.data.reject_num
            this.form.reject_type = res.data.reject_type
            this.getHouse()
            this.getWareColor(res.data.purchase_ware_id)
        },
        async getHouse() {
            this.houseList = []
            if(!this.purchase_test_code) return
            var res = await this.$API.business.inspect.order.detail.get({test_code: this.purchase_test_code});
            if(res.code == 1){
                this.form.purchase_test_id = res.data.id
                this.houseList = res.data.purchase_ware_code
            }else{
                this.$alert(res.message, "提示", {type: 'error'})
            }
        },
        // 入库选择
        async testChange(){
            this.form.goods_code = ""
            this.form.goods_name = ""
            this.form.goods_id = ""
            this.form.color_id = ""
            this.form.color = ""
            this.form.flower = ""
            this.samplecolorList = []
            this.flowerList = []
            this.getWareColor(this.form.purchase_ware_id)
        },
        // 获取入库单下颜色
        async getWareColor(e){
            if(!e) return
            var res = await this.$API.business.machining.house.detail.get({id: e});
            console.log(res);
            this.form.goods_code = res.data.goodsName?.code
            this.form.goods_name = res.data.goods_name
            this.form.goods_id = res.data.goods_id
            let sample = []
            let color = res.data.select_colors.split(',')
            color.map(item=>{
                sample.push({
                    name: item
                })
            })
            this.samplecolorList = sample
            this.flowerList = res.data.select_flower_param
        },
        // 折码
        async getFolding(){
            var res = await this.$API.basic.folding.all.get()
            this.foldingList = res.data
            res.data.map(item => {
                if (item.is_default == 1) {
                    this.form.folding_id = item.id
                    this.up_limit = item.up_limit
                    this.down_limit = item.down_limit
                    this.foldChange(item.id)
                }
            })
        },
        foldChange(e){
            this.folding_num = this.foldingList.find(item => item.id == e).val
            this.total()
        },
        weightInput(e){ 
            this.form.net_weight = e
            this.total()
        },
        // 剪数输入
        cutInputChange(e){
            this.form.check_num = Number(this.read_number) - Number(e)
            this.form.meter = this.form.check_num
            this.total()
        },
        numberInput(e){ 
            this.read_number = e
            this.form.check_num = e - this.form.cut
            this.cutInputChange(this.form.cut)
            this.total()
        },  
        total(){
            let code = 0
            if(this.form.meter && this.form.folding_id){
                code = (Number(this.form.meter) / Number(this.folding_num)).toFixed(2)
                this.form.code_number = (Math.ceil(parseFloat(code) * 10 - 0.0000001) / 10).toFixed(1); 
            }
            if(this.form.net_weight && this.form.meter){
                this.form.gram_weight = ((Number(this.form.net_weight) / Number(this.form.meter)) * 1000).toFixed(1)
            }
        },
        // 颜色
        async getColor(){
            var res = await this.$API.basic.color.all.get()
            let array = []
            res.data.map(item=>{
                array.push({
                    id: item.id,
                    name: `【${item.color_code}】 ${item.name}`,
                    zh_name: item.zh_name
                })
            })
            this.colorList = array
        },
        // 不良品
        async getReject(){
            var res = await this.$API.basic.reject.all.get()
            this.rejectList = res.data
        },


        // 来数回车
        comeEnter(){
            this.$nextTick(() => {
                this.$refs.cutInput.focus()
            })
        },
        is_dialog(){
            let down_limit = Number(this.form.meter) < Number(this.down_limit)
            let up_limit = Number(this.form.meter) > Number(this.up_limit)
            if(down_limit && this.form.meter){
                this.dialog.dialog = true
                this.dialogTitle = `米数小于${this.down_limit}米,当前米数为${this.form.meter}米，请确认是否继续提交？`
            }else if(up_limit && this.form.meter){ 
                this.dialog.dialog = true
                this.dialogTitle = `米数大于${this.up_limit}米,当前米数为${this.form.meter}米，请确认是否继续提交？`
            }else {
                this.submit()
            }
        },
        // 数据提交
        async submit(){
            this.dialog.dialog = false
            this.$refs.dialogForm.validate(async (valid) => {
                if (valid) {
					this.isSaveing = true;
                    var res = await this.$API.business.inspect.execute.edit.post(this.form)
					this.isSaveing = false;
                    if (res.code == 1) {
                        this.visible = false;
                        this.$emit('success', this.form, this.mode)
                        this.$message.success("操作成功")
                    } else {
                        this.$alert(res.message, "提示", {type: 'error'})
                    }
                } else {
					return false;
				}
            })
        },
        colorChange(e){
            let color = e.split('#')[1]
            this.form.color_id = this.colorList.find(item => item.zh_name == color).id
        },
    }
}
</script>

<style >
.el-input-group__append{
    color: #098;
}
</style>
